//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.xnx3.j2ee.init;

import com.xnx3.ConfigManagerUtil;
import com.xnx3.j2ee.Global;
import com.xnx3.j2ee.generateCache.Role;
import com.xnx3.j2ee.generateCache.SmsLog;
import com.xnx3.j2ee.generateCache.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.AttachmentUtil;
import com.xnx3.j2ee.util.ConsoleUtil;
import com.xnx3.j2ee.util.SystemUtil;
import com.xnx3.net.OSSUtil;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class InitApplication implements CommandLineRunner {
    @Resource
    private SqlService sqlService;
    @Value("${spring.datasource.driver-class-name}")
    private String databaseSourceDriverClassName;

    public InitApplication() {
    }

    public void run(String... args) throws Exception {
        ConsoleUtil.debug("项目启动后开启自动初始化缓存数据加载.");
        boolean checkDb = ConfigManagerUtil.getSingleton("systemConfig.xml").getValue("startAutoCheck.db").equals("true");

        try {
            if (checkDb) {
                this.checkDb();
            }
        } catch (NoSuchBeanDefinitionException var6) {
            if (checkDb) {
                ConsoleUtil.info("检测到spring中没有sqlService这个bean，也就是当前项目未使用数据库！数据库自动检测略过");
            }
        }

        try {
            new SmsLog();
        } catch (Throwable var5) {
        }

        try {
            new User();
        } catch (Throwable var4) {
        }

    }

    public void checkDb() {
        boolean useDB = false;
        if (this.databaseSourceDriverClassName.equals("org.sqlite.JDBC")) {
            ConsoleUtil.info("Using the database : Sqlite");
            useDB = true;
        } else {
            ConsoleUtil.info("Using the database : Mysql");
            List<Map<String, Object>> map = this.sqlService.findMapBySqlQuery("SHOW TABLES LIKE '%system%'");
            if (map.size() > 0) {
                useDB = true;
            }
        }

        if (useDB) {
            this.readSystemTable();

            try {
                (new Role()).role(this.sqlService);
            } catch (Throwable var3) {
                ConsoleUtil.debug("权限系统异常:" + var3.getMessage() + "，如果您当前项目使用不到权限编辑操作，此项忽略即可");
            }
        } else {
            Global.databaseCreateFinish = false;
            Global.databaseCreateFinish_explain = "数据库异常：请将数据库中的初始数据导入，数据文件地址  https://github.com/xnx3/iw/blob/master/iw.sql";
            ConsoleUtil.debug(Global.databaseCreateFinish_explain);
        }

    }

    public void readSystemTable() {
        Global.system.clear();
        ConsoleUtil.debug("开始装载System数据表信息");
        List<Map<String, Object>> list = this.sqlService.findMapBySqlQuery("SELECT name,value FROM `system`");

        for(int i = 0; i < list.size(); ++i) {
            Map<String, Object> map = (Map)list.get(i);
            String name = map.get("name").toString();
            String value = map.get("value") != null ? map.get("value").toString() : "";
            Global.system.put(name, value);
            ConsoleUtil.debug(name + "=" + value);
        }

        ConsoleUtil.info("system 表数据载入内存完毕，共" + list.size() + "条数据");
    }

    /** @deprecated */
    public void initOssConfig() {
        ConsoleUtil.info("initOssConfig start...");

        try {
            new OSSUtil();
        } catch (Exception var2) {
        }

        OSSUtil.accessKeyId = SystemUtil.get("ALIYUN_ACCESSKEYID");
        OSSUtil.accessKeySecret = SystemUtil.get("ALIYUN_ACCESSKEYSECRET");
        OSSUtil.bucketName = SystemUtil.get("ALIYUN_OSS_BUCKETNAME");
        OSSUtil.endpoint = SystemUtil.get("ALIYUN_OSS_ENDPOINT");
        OSSUtil.url = AttachmentUtil.netUrl();
        boolean checkOss = ConfigManagerUtil.getSingleton("systemConfig.xml").getValue("startAutoCheck.oss").equals("true");
        if (checkOss) {
            if (OSSUtil.accessKeyId != null && OSSUtil.accessKeySecret.length() >= 10) {
                ConsoleUtil.info("initOssConfig finish.");
            } else {
                ConsoleUtil.info("OSS对象存储初始化时，accessKeyId 或 accessKeyId 无有效值（字符小于10）");
            }
        }
    }
}
